#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QTextCodec>
#include<QString>
#include<QFileDialog>
#include<QDebug>
#include<stdio.h>
#include<string.h>
#define cout qDebug()
QTextCodec *codec;//字符编码转换的全局变量
//字符编码的枚举
enum MyCode
{
    utf_8,
    gbk
} flag;

QString path;
bool ifNew = 0; //判断是否是新打开

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    codec = QTextCodec::codecForName("GBK");
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_actionutf_8_triggered()
{
    flag = utf_8;
    ui->label->setText("当前以utf-8编码显示");
}


void MainWindow::on_actiongbk_triggered()
{
    flag = gbk;
    ui->label->setText("当前以gbk编码显示");
}

void MainWindow::on_actionopen_triggered()
{
    //1.获取文件路径名
    path = QFileDialog::getOpenFileName();
    if(path.isEmpty())
    {
        cout<<"file path error";
        return;
    }
    char *filename = codec->fromUnicode(path).data();
    //2.打开文件
    FILE *fp;
    fp = fopen(filename,"rb");
    if(fp == NULL)
    {
        cout<<"open file error";
        return;
    }
    //3.将文件内容复制到text框里
    char buf[1024];
    QString str;
    while(1)
    {
       memset(buf,0,sizeof(buf));
       fgets(buf,sizeof(buf),fp);
       if(feof(fp))
           break;
       str = str + buf;
    }
    ui->textEdit->setText(str);
    //4.关闭文件
    fclose(fp);
}

void MainWindow::on_actionsaveas_triggered()
{
    //1.获取保存文件的路径
    path = QFileDialog::getSaveFileName();
    if(path.isEmpty())
    {
        cout<<"get Save path error";
        return;
    }
    const char *fileName = codec->fromUnicode(path).data();
    //2.打开保存文件路径
    FILE *fp = NULL;
    fp = fopen(fileName,"wb");
    if(fp == NULL)
    {
        cout<<"fopen error";
        return;
    }
    //3.将textEdit的内容复制到文件里
    QString str = ui->textEdit->toPlainText();
    const char* buf = str.toStdString().data();
    fputs(buf,fp);
    //4.关闭文件
    fclose(fp);
}
//撤销
void MainWindow::on_actionundo_triggered()
{
    ui->textEdit->undo();
}
//复制
void MainWindow::on_actioncopy_triggered()
{
    ui->textEdit->copy();
}
//粘贴
void MainWindow::on_actionpaste_triggered()
{
    ui->textEdit->paste();
}
//剪切
void MainWindow::on_actioncut_triggered()
{
    ui->textEdit->cut();
}
//编译
void MainWindow::on_actioncompile_triggered()
{
    //1.获取path文件目录，并替换.c获取执行文件目录
    QString exe = path;
    exe.replace(".c","");
    //2.编译文件
    QString cmd = QString("gcc %1 -o %2").arg(path).arg(exe);
    cout<<cmd;
    int ret = system(codec->fromUnicode(cmd).data());
    if(ret != 0)
    {
        cmd = QString("cmd /k gcc %1 -o %2").arg(path).arg(exe);
        system(codec->fromUnicode(cmd).data());
        return;
    }
    //3.执行文件
    QString target = QString("cmd /k %1").arg(exe);
    system(codec->fromUnicode(target).data());
}
//退出
void MainWindow::on_actionexit_triggered()
{
    exit(0);
}
//新建
void MainWindow::on_actionnew_triggered()
{
    //清空textEdit和path路径
    path = "";
    ui->textEdit->clear();
    ifNew = 1;
}
//保存
void MainWindow::on_actionsave_triggered()
{
    char* FileName = NULL;
    FILE *fp = NULL;
    //如果新建的textEdit则弹出FileDialog建立路径进行保存
    if(ifNew)
    {
        //1.获得保存路径
        path = QFileDialog::getSaveFileName();
        if(path == "")
        {
            cout<<"path FileDialog err";
            return;
        }
        //2.打开文件路径
        FileName = codec->fromUnicode(path).data();
        fp = NULL;
        fp = fopen(FileName,"wb");
        if(fp == NULL)
        {
            cout<<"fopen err";
            return;
        }
        //3.将text中的内容保存到文件
        QString str = ui->textEdit->toPlainText();
        const char *buf = str.toStdString().data();
        fputs(buf,fp);
        //4.关闭文件
        fclose(fp);
    }
    //如果是打开路径就保存当前路径
    else
    {
        //2.打开文件路径
        FileName = codec->fromUnicode(path).data();
        fp = NULL;
        fp = fopen(FileName,"wb");
        if(fp == NULL)
        {
            cout<<"fopen err";
            return;
        }
        //3.将text中的内容保存到文件
        QString str = ui->textEdit->toPlainText();
        const char *buf = str.toStdString().data();
        fputs(buf,fp);
        //4.关闭文件
        fclose(fp);
    }
    ifNew = 0;//保存后就不是新建的text
}
